def area(a,b):
    return a*b

a = 10
b = 20
print(area(a,b))
print(abs(-100))

def my_abs(x):
    if not isinstance(x,(int,float)):
        raise TypeError('bad type')
    if x>0:
        return x
    else:
        return -x
    
print(my_abs(-10))
s = "aaaa"
print(isinstance(s,(str,int,float))) # 类型检查
# print(my_abs('aaa'))

# 注意：在python函数的默认值是在定义的时候就被初始化了，并不是在调用的时候才初始化，此时就需要特判
def add_end(L=None):
    if L is None:
        L = []
    L.append('end')
    return L
# 定义默认参数要牢记一点：默认参数必须指向不变对象！否则会有问题
print(add_end())
print(add_end())
print(add_end())
print(add_end())

# 定义可变参数
def cal(*number):
    sum = 0
    for x in number:
        sum += x
    return sum
def cal2(number):
    pass

cal(1,2,3,4)
cal(1,24)
l2 = [1,2,3,4,5,7,7,8]
# 将l2这个list中的所有元素作为可变参数传进去
print(cal(*l2)) # 这种调用方式，只对具有可变参数的函数有效

# cal2(*l2) #TypeError



# 关键字参数 这里的**kw会自动组装成一个字典。并且这个**kw是外部参数的拷贝。
def person(name,age,**kw):
    print("name: ",name,"age: ",age, "other:", kw)
    print('name: %s ,age: %d ' % (name,age) + 'other',kw)

def people(name,age,**other):
    print("I am People. name: ",name,"age: ",age, "other: ",other)

name = 'jack'
age = 34
other = {"address:":"beijing","rich":88}
person(name,age,**other)

people(name,age,**other)

# 当前面没有可变参数时，命名关键字参数前面要加上*
def people1(name,age,*,grade = 55):
    print("I am people1. name: ",name, "age: ",age,"grade: ",grade)

dict1 = {"grade":999}
people1("name",45,**dict1)


# 当前面有了可变参数时，命名关键字参数前面就不需要加上*了
def people2(name,*age,  grade = 16):
    print("I am people2. name: ",name,"age: ",age,"grade: ",grade)

dict2 = {"grade":9888}
list2 = [1,2,4,5,6,7,7,8,0]
people2("jack",*list2,**dict2)


def people3(*,address):
    print("I am people3, address: ",address)
# 3种调用方式
dict3 = {"address":"chengdu"}
people3(**dict3)
people3(**{"address":"chengdu"})
people3(address="xian")

def f1(a, b, c=0, *args, **kw):
    print('a =', a, 'b =', b, 'c =', c, 'args =', args, 'kw =', kw)

def f2(a, b, c=0, *, d, **kw):
    print('a =', a, 'b =', b, 'c =', c, 'd =', d, 'kw =', kw)

list4 = [3,2,42,4,24,242]

f1(1,2,)
f2()



# 主程序入口
# if __name__ == '__main__':

#     pass


